package realm;

import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.crypto.hash.SimpleHash;

/**
 * @author dayu
 * @create 2018/12/10 17:47
 * @Description PasswordService :对明文密码进行加密，以及判断密码是否匹配
 * 实现类：DefaultPasswordService sha-256 500000
 * 该类为自定义实现类：
 */
public class MyPasswordServiceImpl implements PasswordService {

    private String salt;
    private String algorithmName;
    private int hashIterations;

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getAlgorithmName() {
        return algorithmName;
    }

    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    public int getHashIterations() {
        return hashIterations;
    }

    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    @Override
    public String encryptPassword(Object password) throws IllegalArgumentException {

        return new SimpleHash(algorithmName, password, salt, hashIterations).toBase64();
    }

    @Override
    public boolean passwordsMatch(Object password, String s) {
        String encryptPassword = encryptPassword(password);
        return encryptPassword.equals(s);
    }


    /**
     *
     * @return
     */

}
